//
//  ProcesssView.m
//  LineByLine
//
//  Created by 병철 김 on 11. 10. 7..
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "RangedProgressView.h"


@interface RangedProgressView(SubMethods)
- (void)strokePathFromStart:(CGPoint)start toEnd:(CGPoint)end using:(CGContextRef)context;
@end


@implementation RangedProgressView

@synthesize paragraphs, pageCount;

#pragma mark - initialize
- (id)initWithFrame:(CGRect)frame {
	self = [super initWithFrame:frame];
	if (self) {
        [self setBackgroundColor:[UIColor clearColor]];
        
		pos = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"marker.png"]];
		page = [[UILabel alloc] initWithFrame:CGRectZero];

        [pos setUserInteractionEnabled:YES];
		[page setFont:[UIFont systemFontOfSize:11]];
		[page setBackgroundColor:[UIColor clearColor]];
        [page setTextColor:[UIColor whiteColor]];

		[self addSubview:pos];
		[self addSubview:page];
        
		[pos release];
		[page release];
	}
	return self;
}

- (void)dealloc {
	[paragraphs release];
	[super dealloc];
}

#pragma mark - draw
- (void) drawSentenceBar: (CGRect) rect  {
	CGContextRef context = UIGraphicsGetCurrentContext();
	CGContextSaveGState(context);
	CGContextSetRGBStrokeColor(context, 1.0, 1.0, 1.0, 1.0);
	CGContextSetLineWidth(context, 1.0);
	
	float ypos = rect.size.height - 2;
	[self strokePathFromStart:CGPointMake(rect.origin.x, ypos) toEnd:CGPointMake(rect.origin.x + rect.size.width, ypos) using:context];
	
	for (int i=0; i<[paragraphs count]; i++) {
		id sentence = [paragraphs objectAtIndex:i];
		int sid = [[sentence valueForKey:@"sid"] intValue];
		
		float xpos = (sid / (float)pageCount) * rect.size.width;
		float startYpos = rect.origin.y + 18;
		float endYpos = startYpos + 4;
		[self strokePathFromStart:CGPointMake(xpos, startYpos) toEnd:CGPointMake(xpos, endYpos) using:context];
	}
	
	CGContextRestoreGState(context);
}

- (void)drawRect:(CGRect)rect {
	[self drawSentenceBar: rect];
}

- (void)strokePathFromStart:(CGPoint)start toEnd:(CGPoint)end using:(CGContextRef)context {
	CGContextMoveToPoint(context, start.x, start.y);
	CGContextAddLineToPoint(context, end.x, end.y);
	CGContextStrokePath(context);
}

#define MARKER_WIDTH 40
- (void)drawMarkerAt:(int) _page {
	float width = self.frame.size.width;
	float xpos = width * (_page / (float)pageCount);
    
	if ((width - xpos) < MARKER_WIDTH) {
		[pos setImage:[UIImage imageNamed:@"marker2.png"]];
		xpos = xpos - MARKER_WIDTH;
        isMarker2 = YES;
	} else {
		[pos setImage:[UIImage imageNamed:@"marker.png"]];
        isMarker2 = NO;
	}
	
	pos.frame = CGRectMake(xpos, 0, MARKER_WIDTH, 20);
	page.frame = CGRectMake(xpos + 11, 0, 30, 15);
	
	NSString *strPage = [[NSString alloc] initWithFormat:@"%d", _page];
	[page setText:strPage];
    [strPage release];
}

#pragma mark - touch event
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    NSSet *targetTouches = [event touchesForView:pos];
    if ([targetTouches count] <= 0) {
        return;
    }
    UITouch *touch = [targetTouches anyObject];
    touchPosX = [touch locationInView:pos].x;
    isTouching = YES;
}

- (int) pageUsingLocation:(float) posX {
    float width = self.frame.size.width;
    int targetPage;
    if (isMarker2) {
        targetPage = (int)(pageCount * ((posX + touchPosX)/width));
    } else {
        targetPage = (int)(pageCount * ((posX - touchPosX)/width));
    }
    return MAX(0, MIN(pageCount, targetPage));
}

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
    if (!isTouching) {
        return;
    }
    NSSet *targetTouches = [event touchesForView:pos];
    UITouch *touch = [targetTouches anyObject];
    CGPoint location = [touch locationInView:self];
    int movePage = [self pageUsingLocation:location.x];
    [self drawMarkerAt:movePage];
}

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
    isTouching = NO;
}
@end
